/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package XML;

import db.definitions.ColumnDefinition;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 *
 * @author abracadabuda
 */
public class XmlConfig {

    private static final String fileName = "config\\config.xml"; //config file
    Element root;
    Document document;
    private String schema_name;
    private String tables_schema;
    private String table_objects;
    private ArrayList<ColumnDefinition> table_objects_pks = new ArrayList<ColumnDefinition>();
    private String table_users;
    private ArrayList<ColumnDefinition> table_users_pks = new ArrayList<ColumnDefinition>();
    private String username;
    private String password;

    private void openXml() { //open XML, if not created, create it!
        try {
            SAXReader reader = new SAXReader();
            document = reader.read(fileName);
            root = document.getRootElement();
        } catch (DocumentException ex) {
            document = DocumentHelper.createDocument();
            root = document.addElement("config");
        }
    }

    private void saveXML() { //save XML structure into file
        try {
            FileWriter out;
            out = new FileWriter(fileName);
            document.write(out);
            out.close();
        } catch (IOException ex) {
            System.out.println("Error! File can not be saved! Check the path to the file!");
        }
    }

    /**
     * loads XML config (tables, schemaName, default user/password, ...)
     */
    
    public void loadConfig() {
        openXml();
        schema_name = root.elementTextTrim("schema_name");

        tables_schema = root.element("tables").attributeValue("schema");
        table_objects = root.element("tables").element("objects").attributeValue("name");
        for (Iterator iterElem = root.element("tables").element("objects").elementIterator("pk"); iterElem.hasNext();) {
            Element objElem = (Element) iterElem.next();
            ColumnDefinition col = new ColumnDefinition();
            col.setColName(objElem.attributeValue("name"));
            col.setType("int4");//objElem.attributeValue("type"));
            table_objects_pks.add(col);
        }

        table_users = root.element("tables").element("users").attributeValue("name");
        for (Iterator iterElem = root.element("tables").element("users").elementIterator("pk"); iterElem.hasNext();) {
            Element objElem = (Element) iterElem.next();
            ColumnDefinition col = new ColumnDefinition();
            col.setColName(objElem.attributeValue("name"));
            col.setType(objElem.attributeValue("type"));
            table_users_pks.add(col);
        }

        username = root.element("first_user").elementTextTrim("username");
        password = root.element("first_user").elementTextTrim("password");
    }

    /**
     * saves the config from attributes
     */
    
    public void saveConfig() {
        openXml();

        root.element("schema_name").setText(schema_name);
        root.element("tables").addAttribute("schema", tables_schema);
        root.element("tables").element("objects").addAttribute("name", table_objects);
        for (Iterator i = root.element("tables").element("objects").elements("pk").iterator(); i.hasNext();) {
            Element e = (Element) i.next();
            root.element("tables").element("objects").remove(e);
        }
        for (ColumnDefinition col : table_objects_pks) {
            Element pk = root.element("tables").element("objects").addElement("pk");
            pk.addAttribute("name", col.getColName());
            pk.addAttribute("type", col.getType());
        }

        root.element("tables").element("users").addAttribute("name", table_users == null ? "" : table_users);
        for (Iterator i = root.element("tables").element("users").elements("pk").iterator(); i.hasNext();) {
            Element e = (Element) i.next();
            root.element("tables").element("users").remove(e);
        }
        if (table_users != null) {
            for (ColumnDefinition col : table_users_pks) {
                Element pk = root.element("tables").element("users").addElement("pk");
                pk.addAttribute("name", col.getColName());
                pk.addAttribute("type", col.getType());
            }
        }
        
        root.element("first_user").element("username").setText(username == null ? "admin" : username); //default is admin
        root.element("first_user").element("password").setText(password == null ? "admin" : password);
        saveXML();
    }

    public String getSchema_name() {
        return schema_name;
    }

    public void setSchema_name(String schema_name) {
        this.schema_name = schema_name;
    }

    public String getTables_schema() {
        return tables_schema;
    }

    public void setTables_schema(String tables_schema) {
        this.tables_schema = tables_schema;
    }

    public String getTable_objects() {
        return table_objects;
    }

    public void setTable_objects(String table_objects) {
        this.table_objects = table_objects;
    }

    public String getTable_users() {
        return table_users;
    }

    public void setTable_users(String table_users) {
        this.table_users = table_users;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public ArrayList<ColumnDefinition> getTable_objects_pks() {
        return table_objects_pks;
    }

    public void setTable_objects_pks(ArrayList<ColumnDefinition> table_objects_pks) {
        this.table_objects_pks = table_objects_pks;
    }

    public ArrayList<ColumnDefinition> getTable_users_pks() {
        return table_users_pks;
    }

    public void setTable_users_pks(ArrayList<ColumnDefinition> table_users_pks) {
        this.table_users_pks = table_users_pks;
    }
    
    /**
     * adds column and its type as PK to table objects
     * @param colName column name of PK
     * @param type column type of PK
     */
    
    public void addPkObjects(String colName, String type) {
        ColumnDefinition col = new ColumnDefinition();
        col.setColName(colName);
        col.setType(type);
        table_objects_pks.add(col);
    }

    /**
     * adds column and its type as PK to table users
     * @param colName column name of PK
     * @param type column type of PK
     */
        
    public void addPkUsers(String colName, String type) {
        ColumnDefinition col = new ColumnDefinition();
        col.setColName(colName);
        col.setType(type);
        table_users_pks.add(col);
    }
}
